package com.tyf.bookreader.controller;

import com.tyf.bookreader.dto.*;
import com.tyf.bookreader.service.BookService;
import com.tyf.bookreader.component.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.security.NoSuchAlgorithmException;
import java.util.List;

/**
 * @Description 小说模块
 * @Author shallow
 * @Date 2023/4/1 18:06
 */
@Api(tags = "小说模块")
@RestController
@RequestMapping("/book")
public class BookController {
    @Autowired
    private BookService bookService;

    /**
     * 查看小说分类列表
     * @return
     */
    @ApiOperation(value = "查看小说分类列表")
    @GetMapping("category/list")
    public Result<List<BookCategoryDto>> listCategory(){
        return bookService.listCategory();
    }

    @ApiOperation(value = "查看小说信息")
    @GetMapping("/{id}")
    public Result<BookInfoDto> getBookById(@PathVariable("id")Long bookId){
        return bookService.getBookById(bookId);
    }

    @ApiOperation(value = "增加小说点击量接口")
    @PostMapping("visit")
    public Result addVisitCount(Long bookId){
        return bookService.addVisitCount(bookId);
    }

    @ApiOperation(value = "小说最新章节相关信息查询接口")
    @GetMapping("last_chapter_info/{bookId}")
    public Result<LastChapterInfoDto> getLastChapterAbout(@PathVariable("bookId") Long bookId){
        return bookService.getLastChapterAbout(bookId);
    }

    @ApiOperation(value = "小说推荐列表查询接口")
    @GetMapping("rec_list/{bookId}")
    public Result<List<BookInfoDto>> listRecBooks(@PathVariable("bookId")Long bookId) throws NoSuchAlgorithmException {
        return bookService.listRecBooks(bookId);
    }

    @ApiOperation(value = "小说章节列表查询接口")
    @GetMapping("chapter_list/{bookId}")
    public Result<List<ChapterDto>> listChapters(@PathVariable("bookId") Long bookId){
        return bookService.listChapters(bookId);
    }

    @ApiOperation(value = "章节信息获取接口")
    @GetMapping("content/{chapterId}")
    public Result<ContentDto> getContentByChapterId(@PathVariable("chapterId")Long chapterId){
        return bookService.getContentByChapterId(chapterId);
    }

    @ApiOperation(value = "获取上一章节ID接口")
    @GetMapping("pre_chapter_id/{chapterId}")
    public Result<Long> getPreChapterId(@PathVariable("chapterId")Long chapterId){
        return bookService.getPreChapterId(chapterId);
    }

    @ApiOperation(value = "获取下一章节ID接口")
    @GetMapping("next_chapter_id/{chapterId}")
    public Result<Long> getNextChapterId(@PathVariable("chapterId")Long chapterId){
        return bookService.getNextChapterId(chapterId);
    }

    @ApiOperation(value = "获取本章节内容")
    @GetMapping("read/{bookId}/{chapterId}")
    public Result<ContentDto> getContent(@PathVariable String bookId, @PathVariable String chapterId){
        boolean flag = bookService.checkBookPurchase(bookId);
        return bookService.getContent(bookId,chapterId);
    }
}
